package com.cddata;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.util.Arrays;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import com.cddata.util.DBOperation;

@SuppressWarnings("serial")
public class LoadCDData extends JPanel implements ActionListener {
	
	public LoadCDData() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add(DBOperation.getHelperBean().createHeaderPanel("Load from CD to file...", null));
		panel.add(buttonPanel());
		add( panel );
		setVisible(true);
	}
	public void actionPerformed( ActionEvent ae ) {
		String action = ae.getActionCommand();
		if( "Load...".equalsIgnoreCase(action)) {
			try{
				if( "".equals(fileName.getText().trim())) {
					alert("Please Enter a filename to save with");
					return;
				}
				performOperation();
			}catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private JPanel buttonPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add( new JLabel("Select a Drive:"));
		panel.add(getCDDriveLetter());
		panel.add( new JLabel("Filename to be saved as:"));
		panel.add(fileName);
		JButton submit = new JButton("Load...");
		submit.addActionListener(this);
		panel.add(submit);
		return panel;
	}
	
	private void alert(String aMessage) {
		JOptionPane.showMessageDialog(this, aMessage);
	}

	private JComboBox getCDDriveLetter() {
		List <File>files = Arrays.asList(File.listRoots());
		for (File f : files) {
			String s1 = FileSystemView.getFileSystemView().getSystemDisplayName (f);
			if( !"".equalsIgnoreCase(s1)) {
				driveCombo.addItem(s1);
			}
		}
		return driveCombo;
	}

	private void performOperation () throws Exception{
		//		load the cd drive and search all the dir and file and save the path into the text file 
		String driveLetter = driveCombo.getSelectedItem().toString();
		int fst = driveLetter.indexOf("(");
		int lst = driveLetter.lastIndexOf(")");
		driveLetter = driveLetter.substring( fst+1, lst  );
		File directory = new File(  driveLetter+ "\\");
		String path = "./files";
		// directory int whih the file is to be stored
		File dirToSave = new File( path);
		// if the directory does not exist , create it
		if( !dirToSave.exists()){
			dirToSave.mkdirs();
		}
		File txtToSave = new File( path + "\\" + fileName.getText() + ".txt");		
		FileWriter fw = new FileWriter(txtToSave);
		fileCount = 0;
		thumbsCount = 0;
		visitAllDirsAndFiles( directory.listFiles(), fw );
		fw.close();
		alert(" Total file(s) in the CD "+ fileCount +" with thumb(s) Count = " + thumbsCount);
	}

	private void visitAllDirsAndFiles(File dir[], FileWriter fw ) {		
		try{
			// variable to get the path string and write the same to the file;
			if(dir!= null) {
				for( int i = 0; i< dir.length; i++ ) {
					File file = dir[i];
					if( file.isDirectory() ) {
						visitAllDirsAndFiles(file.listFiles(), fw );
					}else {
						// check if the file is thumbs.db which is to be ignored from the file list
						if( !file.getName().equalsIgnoreCase("thumbs.db" )) {
							// write the file path and the size of the file 
							// then flush the data in order for the data to be stored to file
							fw.write( file.getAbsolutePath() + "\t" + file.length() + "\n");
							fileCount++;
							fw.flush();
						}else {
							thumbsCount++;
						}
					}
				}   
			}

		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	JComboBox driveCombo = new JComboBox();
	JTextField fileName = new JTextField(15);
	int thumbsCount = 0;
	int fileCount = 0;
}
